home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / ansi2knr.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  8KB  |  301 lines

  1. /* Copyright (C) 1989, 1991 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* ansi2knr.c */
  20. /* Convert ANSI function declarations to K&R syntax */
  21. #include "stdio_.h"
  22. #include <ctype.h>
  23. #include "string_.h"
  24. #include "malloc_.h"
  25.  
  26. /* Usage:
  27.     ansi2knr input_file output_file
  28.  * If no output_file is supplied, output goes to stdout.
  29.  * There are no error messages.
  30.  *
  31.  * ansi2knr recognizes functions by seeing a non-keyword identifier
  32.  * at the left margin, followed by a left parenthesis,
  33.  * with a right parenthesis as the last character on the line.
  34.  * It will recognize a multi-line header if the last character
  35.  * on each line but the last is a left parenthesis or comma.
  36.  * These algorithms ignore whitespace and comments, except that
  37.  * the function name must be the first thing on the line.
  38.  * The following constructs will confuse it:
  39.     - Any other construct that starts at the left margin and
  40.         follows the above syntax (such as a macro or function call).
  41.     - Macros that tinker with the syntax of the function header.
  42.  */
  43.  
  44. /* Scanning macros */
  45. #define isidchar(ch) (isalnum(ch) || (ch) == '_')
  46. #define isidfirstchar(ch) (isalpha(ch) || (ch) == '_')
  47.  
  48. main(argc, argv)
  49.     int argc;
  50.     char *argv[];
  51. {    FILE *in, *out;
  52. #define bufsize 5000            /* arbitrary size */
  53.     char *buf;
  54.     char *line;
  55.     switch ( argc )
  56.        {
  57.     default:
  58.         printf("Usage: ansi2knr input_file [output_file]\n");
  59.         exit(0);
  60.     case 2:
  61.         out = stdout; break;
  62.     case 3:
  63.         out = fopen(argv[2], "w");
  64.         if ( out == NULL )
  65.            {    fprintf(stderr, "Cannot open %s\n", argv[2]);
  66.             exit(1);
  67.            }
  68.        }
  69.     in = fopen(argv[1], "r");
  70.     if ( in == NULL )
  71.        {    fprintf(stderr, "Cannot open %s\n", argv[1]);
  72.         exit(1);
  73.        }
  74.     fprintf(out, "#line 1 \"%s\"\n", argv[1]);
  75.     buf = malloc(bufsize);
  76.     line = buf;
  77.     while ( fgets(line, (unsigned)(buf + bufsize - line), in) != NULL )
  78.        {    switch ( test1(buf) )
  79.            {
  80.         case 1:            /* a function */
  81.             convert1(buf, out);
  82.             break;
  83.         case -1:        /* maybe the start of a function */
  84.             line = buf + strlen(buf);
  85.             if ( line != buf + (bufsize - 1) ) /* overflow check */
  86.                 continue;
  87.             /* falls through */
  88.         default:        /* not a function */
  89.             fputs(buf, out);
  90.             break;
  91.            }
  92.         line = buf;
  93.        }
  94.     if ( line != buf ) fputs(buf, out);
  95.     free(buf);
  96.     fclose(out);
  97.     fclose(in);
  98.     return 0;
  99. }
  100.  
  101. /* Skip over space and comments, in either direction. */
  102. char *
  103. skipspace(p, dir)
  104.     register char *p;
  105.     register int dir;            /* 1 for forward, -1 for backward */
  106. {    for ( ; ; )
  107.        {    while ( isspace(*p) ) p += dir;
  108.         if ( !(*p == '/' && p[dir] == '*') ) break;
  109.         p += dir;  p += dir;
  110.         while ( !(*p == '*' && p[dir] == '/') )
  111.            {    if ( *p == 0 ) return p;    /* multi-line comment?? */
  112.             p += dir;
  113.            }
  114.         p += dir;  p += dir;
  115.        }
  116.     return p;
  117. }
  118.  
  119. /*
  120.  * Write blanks over part of a string.
  121.  */
  122. void
  123. writeblanks(start, end)
  124.     char *start;
  125.     char *end;
  126. {    char *p;
  127.     for ( p = start; p < end; p++ ) *p = ' ';
  128. }
  129.  
  130. /*
  131.  * Test whether the string in buf is a function definition.
  132.  * The string may contain and/or end with a newline.
  133.  * Return as follows:
  134.  *    0 - definitely not a function definition;
  135.  *    1 - definitely a function definition;
  136.  *    -1 - may be the beginning of a function definition,
  137.  *        append another line and look again.
  138.  */
  139. test1(buf)
  140.     char *buf;
  141. {    register char *p = buf;
  142.     char *bend;
  143.     char *endfn;
  144.     int contin;
  145.     if ( !isidfirstchar(*p) )
  146.         return 0;        /* no name at left margin */
  147.     bend = skipspace(buf + strlen(buf) - 1, -1);
  148.     switch ( *bend )
  149.        {
  150.     case ')': contin = 1; break;
  151.     case '(':
  152.     case ',': contin = -1; break;
  153.     default: return 0;        /* not a function */
  154.        }
  155.     while ( isidchar(*p) ) p++;
  156.     endfn = p;
  157.     p = skipspace(p, 1);
  158.     if ( *p++ != '(' )
  159.         return 0;        /* not a function */
  160.     p = skipspace(p, 1);
  161.     if ( *p == ')' )
  162.         return 0;        /* no parameters */
  163.     /* Check that the apparent function name isn't a keyword. */
  164.     /* We only need to check for keywords that could be followed */
  165.     /* by a left parenthesis (which, unfortunately, is most of them). */
  166.        {    static char *words[] =
  167.            {    "asm", "auto", "case", "char", "const", "double",
  168.             "extern", "float", "for", "if", "int", "long",
  169.             "register", "return", "short", "signed", "sizeof",
  170.             "static", "switch", "typedef", "unsigned",
  171.             "void", "volatile", "while", 0
  172.            };
  173.         char **key = words;
  174.         char *kp;
  175.         int len = endfn - buf;
  176.         while ( (kp = *key) != 0 )
  177.            {    if ( strlen(kp) == len && !strncmp(kp, buf, len) )
  178.                 return 0;    /* name is a keyword */
  179.             key++;
  180.            }
  181.        }
  182.     return contin;
  183. }
  184.  
  185. convert1(buf, out)
  186.     char *buf;
  187.     FILE *out;
  188. {    char *endfn = strchr(buf, '(') + 1;
  189.     register char *p;
  190.     char **breaks;
  191.     unsigned num_breaks = 2;    /* for testing */
  192.     char **btop;
  193.     char **bp;
  194.     char **ap;
  195. top:    p = endfn;
  196.     breaks = (char **)malloc(sizeof(char *) * num_breaks * 2);
  197.     if ( breaks == 0 )
  198.        {    /* Couldn't allocate break table, give up */
  199.         fprintf(stderr, "Unable to allocate break table!\n");
  200.         fputs(buf, out);
  201.         return -1;
  202.        }
  203.     btop = breaks + num_breaks * 2 - 2;
  204.     bp = breaks;
  205.     /* Parse the argument list */
  206.     do
  207.        {    int level = 0;
  208.         char *end = NULL;
  209.         if ( bp >= btop )
  210.            {    /* Filled up break table. */
  211.             /* Allocate a bigger one and start over. */
  212.             free((char *)breaks);
  213.             num_breaks <<= 1;
  214.             goto top;
  215.            }
  216.         *bp++ = p;
  217.         /* Find the end of the argument */
  218.         for ( ; end == NULL; p++ )
  219.            {    switch(*p)
  220.                {
  221.             case ',': if ( !level ) end = p; break;
  222.             case '(': level++; break;
  223.             case ')': if ( --level < 0 ) end = p; break;
  224.             case '/': p = skipspace(p, 1) - 1; break;
  225.             default: ;
  226.                }
  227.            }
  228.         p--;            /* back up over terminator */
  229.         /* Find the name being declared. */
  230.         /* This is complicated because of procedure and */
  231.         /* array modifiers. */
  232.         for ( ; ; )
  233.            {    p = skipspace(p - 1, -1);
  234.             switch ( *p )
  235.                {
  236.             case ']':    /* skip array dimension(s) */
  237.             case ')':    /* skip procedure args OR name */
  238.                {    int level = 1;
  239.                 while ( level )
  240.                  switch ( *--p )
  241.                    {
  242.                 case ']': case ')': level++; break;
  243.                 case '[': case '(': level--; break;
  244.                 case '/': p = skipspace(p, -1) + 1; break;
  245.                 default: ;
  246.                    }
  247.                }
  248.                 if ( *p == '(' && *skipspace(p + 1, 1) == '*' )
  249.                    {    /* We found the name being declared */
  250.                     while ( !isidfirstchar(*p) )
  251.                         p = skipspace(p, 1) + 1;
  252.                     goto found;
  253.                    }
  254.                 break;
  255.             default: goto found;
  256.                }
  257.            }
  258. found:        if ( *p == '.' && p[-1] == '.' && p[-2] == '.' )
  259.            {    p++;
  260.             if ( bp == breaks + 1 )    /* sole argument */
  261.                 writeblanks(breaks[0], p);
  262.             else
  263.                 writeblanks(bp[-1] - 1, p);
  264.             bp--;
  265.            }
  266.         else
  267.            {    while ( isidchar(*p) ) p--;
  268.             *bp++ = p+1;
  269.            }
  270.         p = end;
  271.        }
  272.     while ( *p++ == ',' );
  273.     *bp = p;
  274.     /* Make a special check for 'void' arglist */
  275.     if ( bp == breaks+2 )
  276.        {    p = skipspace(breaks[0], 1);
  277.         if ( !strncmp(p, "void", 4) )
  278.            {    p = skipspace(p+4, 1);
  279.             if ( p == breaks[2] - 1 )
  280.                {    bp = breaks;    /* yup, pretend arglist is empty */
  281.                 writeblanks(breaks[0], p + 1);
  282.                }
  283.            }
  284.        }
  285.     /* Put out the function name */
  286.     p = buf;
  287.     while ( p != endfn ) putc(*p, out), p++;
  288.     /* Put out the declaration */
  289.     for ( ap = breaks+1; ap < bp; ap += 2 )
  290.        {    p = *ap;
  291.         while ( isidchar(*p) ) putc(*p, out), p++;
  292.         if ( ap < bp - 1 ) fputs(", ", out);
  293.        }
  294.     fputs(")  ", out);
  295.     /* Put out the argument declarations */
  296.     for ( ap = breaks+2; ap <= bp; ap += 2 ) (*ap)[-1] = ';';
  297.     fputs(breaks[0], out);
  298.     free((char *)breaks);
  299.     return 0;
  300. }
  301.